package com.styytech.dadabao.ui.login;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.loopj.android.http.RequestParams;
import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.api.requestresult.RegistResult;
import com.styytech.dadabao.api.response.AbstractResponseResult;
import com.styytech.dadabao.api.templates.HttpRequest;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.constant.ConstantsServerUrl;
import com.styytech.dadabao.exception.ArgumentsException;
import com.styytech.dadabao.exception.RequestException;
import com.styytech.dadabao.utils.CommonUtils;
import com.styytech.dadabao.utils.Md5Utils;
import com.styytech.dadabao.utils.SpUser;
import com.styytech.dadabao.widge.dialog.CustomLoadingDialog;

/**
 * 设置登录密码
 */
@ContentView(R.layout.activity_update_password)
public class UpdateLoginPasswordActivity extends BaseActivity {
    /**
     * 确认修改登录密码
     */
    public final int REQUEST_CODE_UPDATE = 3;

    /*修改登录密码整体布局*/
    @ViewInject(R.id.llyt_update_payfor)
    LinearLayout llyt_update_payfor;

    /*原登录密码*/
    @ViewInject(R.id.et_payfor_old)
    EditText et_payfor_old;
    /*新登录密码*/
    @ViewInject(R.id.et_payfor_new)
    EditText et_payfor_new;
    /*确认登录密码*/
    @ViewInject(R.id.et_payfor_confirm)
    EditText et_payfor_confirm;
    /*确认修改*/
    @ViewInject(R.id.bt_ok)
    Button bt_ok;


    /*上下文*/
    private Context mContext;
    /*loading框*/
    CustomLoadingDialog mdlg;
    private String password = "";// 密码
    private String passwordOk = "";// 确认密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        initView();
        initData();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        DadaApplication.addActivity(this);
        setTopBar("修改密码", View.VISIBLE);

    }

    /**
     * 初始界面设置
     */
    private void initData() {
    }

    @OnClick({R.id.bt_ok})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.bt_ok:// 确认修改
                submitUpdatePayfor();
                break;
        }
    }

    /**
     * 提交修改登录密码
     * payPassword
     * oldPayPassword
     * token
     */
    public void submitUpdatePayfor() {
        String oldPayPassword = et_payfor_old.getText().toString().trim();
        password = et_payfor_new.getText().toString().trim();
        passwordOk = et_payfor_confirm.getText().toString().trim();
        /*简单的条件过滤*/
        if (oldPayPassword.isEmpty()) {
            //密码不能为空
            toast(getResources().getString(R.string.pwd_no_empty_old));
            return;
        } else if (oldPayPassword.length() < 6) {
            //密码不能少于6位
            toast(getResources().getString(R.string.pwd_too_low_old));
            return;
        } else if (password.isEmpty()) {
            //密码不能为空
            toast(getResources().getString(R.string.pwd_no_empty_new));
            return;
        } else if (password.length() < 6) {
            //密码不能少于6位
            toast(getResources().getString(R.string.pwd_too_low_new));
            return;
        } else if (password.length() > 30) {
            //密码长度不能大于30位
            toast(getResources().getString(R.string.pwd_too_much));
            return;
        } else if (passwordOk.isEmpty()) {
            // 确认密码不能为空
            toast(getResources().getString(R.string.pwd_ensure_no_empty));
            return;
        } else if (!password.equals(passwordOk)) {
            //两次密码不一致
            toast(getResources().getString(R.string.pwd_no_agree));
            return;
        }

        // 显示对话框
        mdlg = CommonUtils.getCustomLoadingDialog(context, mdlg, "修改登录密码", true);
        mdlg.show();

        /*获取修改登录密码请求接口*/
        String url = ConstantsServerUrl.getUpdateUserPasswordUrl();
        /*设置参数*/
        RequestParams params = new RequestParams();
        params.put("oldPassword", Md5Utils.hash(oldPayPassword));
        params.put("password", Md5Utils.hash(password));
        params.put("token", SpUser.getUserToken(mContext));
        /*请求校验验证码*/
        requestAuthCode(REQUEST_CODE_UPDATE, url, params);
    }

    /**
     * 请求校验验证码
     *
     * @param rquestCode 请求码
     * @param url        请求的url接口
     * @param params     请求的参数
     */
    public void requestAuthCode(int rquestCode, String url, RequestParams params) {

        //获取验证码
        RegistResult requestResult = new RegistResult(rquestCode, responseResult);
        try {
            new HttpRequest(this).doPost(requestResult, url, params);
        } catch (ArgumentsException e) {
            Log.i("INFO", "1e:" + e);
            e.printStackTrace();
        } catch (RequestException e) {
            Log.i("INFO", "2e:" + e);
            e.printStackTrace();
        }
    }


    /**
     * 注册请求结果处理
     */
    AbstractResponseResult responseResult = new AbstractResponseResult() {

        @Override
        public void executeSuccess(Object object) {
            CommonUtils.closeDialog(mdlg);
            switch ((int) object) {
                case REQUEST_CODE_UPDATE://提交修改的密码成功后
                    CommonUtils.closeDialog(mdlg);
                    toast("修改登录密码成功");
                    DadaApplication.exit();//关闭所有界面
                    SpUser.setUserToken(mContext, null);//删除token
                    startNextActivity(LoginActivity.class);
                    finish();
                    break;
            }

        }

        @Override
        public void executeError(String message, int errorCode) {
            CommonUtils.showToastMsg(context, "" + message);
            CommonUtils.closeDialog(mdlg);
        }

        @Override
        public void HttpError(String message, int errorCode) {
            CommonUtils.closeDialog(mdlg);
            CommonUtils.showToastMsg(context, "" + message);
        }
    };

    /**
     * 启动界面
     *
     * @param cls 需要启动的界面类
     */
    private void startNextActivity(Class<?> cls) {
        Intent intent = new Intent(mContext, cls);
        startActivity(intent);
    }

    /**
     * 提示消息
     *
     * @param str 需要提示的消息
     */
    private void toast(String str) {
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

}

